#![allow(non_snake_case)]
mod checkbox_group;
mod checkbox_item;

pub use checkbox_group::CheckboxGroup;
pub use checkbox_item::CheckboxItem;

use leptos::prelude::*;
use shq_common::prelude::Size;
use thaw_components::*;
use thaw_utils::{class_list, BoxOneCallback, Model};
use wasm_bindgen::JsCast;
#[component]
pub fn Checkbox(
    #[prop(optional, into)] value: Model<bool>,
    #[prop(optional, into)] on_change: Option<BoxOneCallback<bool>>,
    #[prop(optional, into)] size: MaybeProp<Size>,
    #[prop(optional, into)] disabled: MaybeProp<bool>,
    #[prop(optional)] children: Option<Children>,
) -> impl IntoView {
    let on_change = move |ev: web_sys::Event| {
        let val = ev
            .target()
            .and_then(|t| t.dyn_into::<web_sys::HtmlInputElement>().ok())
            .map(|t| t.checked());
        if let Some(on_change) = on_change.as_ref() {
            on_change(val.unwrap_or(false));
        }
    };
    view! {
        <div
            class="form-control min-w-16"
            on:click=move |_| value.set(!value.get())
        >
            <label class=class_list!["cursor-pointer label",move||size.get().map(|s|s.to_class("checkbox"))]>
                <input class="checkbox" type="checkbox" checked=move||value.get()
                    class=("checkbox-checkbox-primary",move||value.get())
                    disabled=move||disabled.get().unwrap_or(false)
                    on:change=on_change
                />
                <OptionComp value=children let:children>
                    <span class="label-text">{children()}</span>
                </OptionComp>
            </label>
        </div>
    }
}
